/*
* Copyright 2010 Dovado FZ-LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*       http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#import "LoginViewController.h"
#import "DovadoAppDelegate.h"
#import "LicenseViewController.h";
#import "RouterViewController.h";
#import "DialogUtils.h"

@implementation LoginViewController
@synthesize mPasswordField;
@synthesize mUserField;
@synthesize mHostField;
/*
 // The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
 - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
 if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
 // Custom initialization
 }
 return self;
 }
 */

 // Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
 - (void)viewDidLoad {
 [super viewDidLoad];
	 NSUserDefaults* userSettings = [NSUserDefaults standardUserDefaults];
	 NSString* hostNameString = [userSettings objectForKey:@"HOST_NAME_PREF"]; 
	 NSString* userString = [userSettings objectForKey:@"USER_PREF"];
	 NSString* passwordString = [userSettings objectForKey:@"PASSWORD_PREF"];
	 
	 if (hostNameString != NULL) {
		 mHostField.text = hostNameString;
	 }
	 
	 if (passwordString != NULL) {
		 mPasswordField.text = passwordString;
	 }
	 if (userString != NULL) {
		 mUserField.text = userString;
	 }
 }


/*
 // Override to allow orientations other than the default portrait orientation.
 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
 // Return YES for supported orientations
 return (interfaceOrientation == UIInterfaceOrientationPortrait);
 }
 */

- (IBAction) onLoginButtonClicked {
	NSLog(@"onLoginButtonClicked");	
	NSLog(@"Username: %@", mHostField.text);
	NSLog(@"Username: %@", mUserField.text);
	NSLog(@"Password: %@", mPasswordField.text);
	
	if (mHostField.text != NULL && [mHostField.text length]>0 && mUserField.text != NULL && [mUserField.text length]>0 &&
		mPasswordField.text != NULL && [mPasswordField.text length]>0) {
		// [self.view removeFromSuperview];
		DovadoAppDelegate *appDelegate = (DovadoAppDelegate*)[[UIApplication sharedApplication] delegate];
		appDelegate.mPassword = mPasswordField.text;
		appDelegate.mUser = mUserField.text;
		appDelegate.mHostAddress = mHostField.text;
		
		[appDelegate loginButtonClicked];
		
	} else {
		[DialogUtils showLoginFailedDialog];
	}		 
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
	//Hide any keyboard showing if any event is received here.
	[mHostField resignFirstResponder];
	[mUserField resignFirstResponder];
	[mPasswordField resignFirstResponder];
}

- (IBAction) onKeyboardDone {
	[mHostField resignFirstResponder];
	[mUserField resignFirstResponder];
	[mPasswordField resignFirstResponder];
}

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
	NSLog(@"viewDidUnload");
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
	[mPasswordField dealloc];
	[mHostField dealloc];
	[mUserField dealloc];
    [super dealloc];
	
}


@end
